<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Generic controller for entity class, CI AR.
 * @author hmQuan
 *
 */
class MY_Controller extends CI_Controller {

	//contructor class
	function __construct() {

		parent::__construct();
		$this->load->model('gcmmodel');
		$this->load->model('t_user');
	}

	/**
	 * General function here
	 */
	
	/**
	 * push notification to client
	 * @param  [type] $user_id [description]
	 * @param  [type] $msg     [description]
	 * @return [type]          [description]
	 */
	public function push_notification_to_client_via_user_id( $user_id = -1, $msg, $type = 3 , $id_go_to = -1){
		if($user_id == -1) return;
		$arrGcm = $this->gcmmodel->get_data_by_property(array(LOGIN_USER_ID => $user_id));
		if(!empty($arrGcm)){
			foreach ($arrGcm as $key => $value) {
				$arr[]  = $value[LOGIN_GCM_ID];
			}
			require_once(APPPATH . 'controllers/gcmusers.php');
			$gcmusers = new GCMUsers();
			return $gcmusers->send_notification_android($arr, $msg, $type , $id_go_to);
		}
	}

	public function push_notification_to_client_via_gcm($gcm_regid, $msg, $type = 3 , $id_go_to = -1){
		$gcm = $this->gcmmodel->get_data_by_property(array(GCM_REGID => $gcm_regid));
		if(!empty($gcm)){
			$user = $gcm[0][LOGIN_USER_ID];
			$this->push_notification_to_client_via_user_id($user, $msg, $type ,$id_go_to);
		}
	}


	/**
	 * 
	 */
	public function check_post_value($post = null){
		// check post is null
		if($post){
			// check value with key data
			if(isset($post[DATA])){
				$data = json_decode($post[DATA], true);
				//check token in server
				if(!empty($data[GCM_REGID]) && isset($data[GCM_REGID])){
					$isRegister = $this->gcmmodel->countRegIdAlreadyExist($data[GCM_REGID]);
					
					if(empty($isRegister)){
						$this->print_result('Device id unregister');exit();
					}
					// 
					return $data;
				}else{
					$this->print_result(array('check_post_value'=>'missing token.'), 0);
				exit();
				}
			}else{

				$this->print_result(array('check_post_value'=>'missing key : data.'), 0);
				exit();
			}
		}else{

			$this->print_result('post null.', 0);
			exit();
		}
	}

	/**
	 * validate user is activity
	 * @param  string  $gcm_regid [description]
	 * @param  string  $user_id   [description]
	 * @return integer            [description]
	 */
	public function is_activity_user($gcm_regid = '', $user_id = ''){
		$is_activity = $this->gcmmodel->is_activity_user($gcm_regid, $user_id);
		return $is_activity;
	}

	public function retrieveUserGcm($gcm = -1){
		if($gcm == -1){
			return 'Params missing value';
		}

		return $this->gcmmodel->getGCMUsers(array(GCM_REGID => $gcm));
	}

	// VALIDATION USER EXIST : exist and is activity
	public function validation_user(){
		$post = $this->input->post();
		$post = $this->check_post_value($post);
		$gcm_regid = $post[GCM_REGID];

		// validation user
		$userInfor = $this->gcmmodel->getGCMUsers(array(GCM_REGID => $gcm_regid));
		if($userInfor == null || count($userInfor) == 0){
			$this->print_result('User does not exits');exit();
		}else {
			$userInfor = $userInfor[0];
			if(!isset($userInfor[USER_ID]) || $userInfor[LOGIN_IS_ACTIVITY] < 1){
				$this->print_result('Login required');exit();
			}
		}
		$post[T_USERS] = $userInfor;
		return $post;
	}

	/**
	 * return result to client
	 */
	public function print_result($data = array(), $success = 0){
		$result = array( SUCCESS => $success, DATA => $data);

		echo json_encode($result);
	}

	public function print_result_array($data){
		echo json_encode($data);
	}
}